package org.dromara.hrp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.hrp.domain.HrpLead;
import org.dromara.hrp.domain.HrpLeadComment;
import org.dromara.hrp.domain.HrpLeadQuote;
import org.dromara.hrp.service.impl.HrpLeadServiceImpl;
import org.dromara.wms.service.WmsStockService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 线索商机
 *
 * @author wenrui
 * @date 2024-03-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hrp/lead")
public class    HrpLeadController extends BaseService {

  private final HrpLeadServiceImpl hrpLeadService;

  /**
   * 查询线索商机列表
   */
  @SaCheckPermission("hrp:lead:list")
  @GetMapping("/list")
  public TableDataInfo<HrpLead> list(HrpLead bo, PageQuery pageQuery) {
    return hrpLeadService.queryPageList(bo, pageQuery);
  }

  /**
   * 导出线索商机列表
   */
  @SaCheckPermission("hrp:lead:export")
  @Log(title = "线索商机", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HrpLead bo, HttpServletResponse response) {
    List<HrpLead> list = hrpLeadService.queryList(bo);
    ExcelUtil.exportExcel(list, "线索商机", HrpLead.class, response);
  }

  /**
   * 获取线索商机详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("hrp:lead:query")
  @GetMapping("/{id}")
  public R<HrpLead> getInfo(@NotNull(message = "主键不能为空")
                            @PathVariable Long id) {
    return R.ok(hrpLeadService.queryById(id));
  }

  /**
   * 新增线索商机
   */
  @SaCheckPermission("hrp:lead:add")
  @Log(title = "线索商机", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody HrpLead bo) {
    return hrpLeadService.insertByBo(bo);
  }

  /**
   * 修改线索商机
   */
  @SaCheckPermission("hrp:lead:edit")
  @Log(title = "线索商机", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrpLead bo) {
    return toAjax(hrpLeadService.updateByBo(bo));
  }

  /**
   * 订单执行
   */
  @SaCheckPermission("hrp:lead:turn")
  @Log(title = "线索执行", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/turnLead")
  public R<Void> turnLead(@Validated(EditGroup.class) @RequestBody HrpLead lead) throws WmsStockService.emptyException {
    return hrpLeadService.turnLead(lead);
  }

  /**
   * 删除线索商机
   *
   * @param ids 主键串
   */
  @SaCheckPermission("hrp:lead:remove")
  @Log(title = "线索商机", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] ids) {
    return toAjax(hrpLeadService.deleteWithValidByIds(List.of(ids), true));
  }

  @SaCheckPermission("hrp:lead:query")
  @GetMapping("/getCommentList/{commentId}")
  public TableDataInfo<HrpLeadComment> getCommentById(@PathVariable Long commentId) {
    return hrpLeadService.getCommentById(commentId);
  }

  /**
   * 查询测试从列表======================Quote表的路由服务==========================
   */
  @SaCheckPermission("hrp:lead:list")
  @GetMapping("/quoteList")
  public TableDataInfo<HrpLeadQuote> quoteList(HrpLeadQuote bo, PageQuery pageQuery) {
    return hrpLeadService.queryQuotePageList(bo, pageQuery);
  }

  @SaCheckPermission("hrp:lead:edit")
  @GetMapping("/getQuoteList/{id}")
  public TableDataInfo<HrpLeadQuote> getQuoteListById(@PathVariable Long id) {
    return hrpLeadService.getQuoteById(id);
  }

  /**
   * 删除测试从
   * @param quoteIds 主键串
   */
  @SaCheckPermission("hrp:lead:remove")
  @Log(title = "测试从", businessType = BusinessType.DELETE)
  @DeleteMapping("/quoteId/{quoteIds}")
  public R<Void> removeQuote(@NotEmpty(message = "主键不能为空") @PathVariable Long[] quoteIds) {
    return toAjax(hrpLeadService.deleteWithValidByQuoteIds(List.of(quoteIds), true));
  }
}






